/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.forward.mobile.optitrans.util;

import com.forward.mobile.optitrans.domain.Arret;
import com.forward.mobile.optitrans.domain.DeviceParam;
import com.forward.mobile.optitrans.domain.Position;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 *
 * @author jbromo
 */
public final class Utils {

    public static List<Position> parseLocation(String value) {
        if (value == null || "none".equalsIgnoreCase(value) || value.length()<4) {
            return new ArrayList<Position>();
        }
        String[] positions = value.split(":");
        
        List<Position> list = new ArrayList<Position>();
        for (String item : positions) {
            Position position = parsePosition(item);
            if (position != null) {
                list.add(position);
            }
        }
        Collections.sort(list);
        return list;

    }

    public static String toStringFormat(List<Position> positions) {
        StringBuilder buffer = new StringBuilder();
        for (Position position : positions) {
            buffer.append(position.toSaveFormat());
            buffer.append("###");

        }
        return buffer.toString();
    }

    public static Position parsePosition(String value) {
        if (value != null && value.length() > 10) {
            String[] item = value.split(",");
            Position p = new Position();
            p.setLatitude(Double.parseDouble(item[0]));
            p.setLongitude(Double.parseDouble(item[1]));
            p.setVitesse(Double.parseDouble(item[2]));
            p.setStatus(item[4]);
            p.setDateGPS(Long.parseLong(item[5]));
            return p;

        }
        return null;
    }

    public static List<Arret> parseArret(String data, DeviceParam deviceParam) {
        List<Arret> arrets = new ArrayList<Arret>();
        if (data == null || "none".equalsIgnoreCase(data) || data.length()<4) {
            return arrets;
        }
        String[] values = data.split(":");
        for (String value : values) {
            try {
                String[] item = value.split(",");
                Arret p = new Arret();
                p.setLatitude(Double.parseDouble(item[0]));
                p.setLongitude(Double.parseDouble(item[1]));
                long start=Long.parseLong(item[2]);
                long end=Long.parseLong(item[3]);
                p.setStart(start);
                p.setEnd(end);
                p.setUuid(deviceParam.getUuid());
                p.setIdDevice(deviceParam.getIdDevice());
                p.setIdVehicule(deviceParam.getIdVehicule());
                p.setIdChauffeur(deviceParam.getIdChauffeur());
                p.setDuree(end-start);
                arrets.add(p);
            } catch (Exception exception) {
            }
        }
        return arrets;
    }

    public static String updateReques() {
        String col = "(latitude,longitude,vitesse,status,date_reception_position,day_itinerary,id_device,id_vehicule,id_chauffeur,id)";
        String onupdate = "latitude=values(latitude),longitude=values(longitude),vitesse=values(vitesse),status=values(status),date_reception_position=values(date_reception_position),id_device=values(id_device),id_vehicule=values(id_vehicule),id_chauffeur=values(id_chauffeur),id=values(id),day_itinerary=concat(day_itinerary,values(day_itinerary))";
        return "insert into position " + col + " values(?,?,?,?,?,?,?,?,?,?) on duplicate key update  " + onupdate;
    }
    public static String deviceRequest(){
        
       return "select p.*,v.immatricule, c.matricule from  device_param p,vehicule v,chauffeur c where uuid_device=? and p.id_vehicule=v.id and p.id_chauffeur=c.id order by p.id desc limit 1";  
      
    }


    public static String compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        String outStr = out.toString("ISO-8859-1");  
        return outStr;
     }
    
    public static String decompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("ISO-8859-1")));
        BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "ISO-8859-1"));
        StringBuilder outStr = new StringBuilder();
        String line;
        while ((line=bf.readLine())!=null) {
          outStr.append(line);
           
        }
       
        return outStr.toString();
     }
}
